package redis;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

public class RedisDemoList {
    // lpush 和 lpushx
    public static void test01(Jedis jedis) {
        jedis.flushAll();
        jedis.lpush("key1", "1", "2", "3");
        System.out.println(jedis.lrange("key1", 0, -1));
        System.out.println(jedis.lpushx("key1", "4"));
        System.out.println(jedis.lpushx("key3", "2", "3"));
    }

    // rpush 和 rpushx
    public static void test02(Jedis jedis) {
        System.out.println(jedis.rpush("key4", "4", "1", "9"));
        jedis.lrange("key4", 0, -1);
        System.out.println(jedis.rpushx("key5", "0", "1"));
        jedis.lrange("key5", 0, -1);
    }

    // lpop 和 rpop
    public static void test03(Jedis jedis) {
        System.out.println(jedis.lpop("key1"));
        System.out.println(jedis.rpop("key1"));
        System.out.println(jedis.lindex("key1", 2));
    }

    // lrem 和 ltrim 和 lset
    public static void test04(Jedis jedis) {
        jedis.lpush("key10", "1", "2", "3", "4", "1", "1");
        System.out.println(jedis.lrem("key10", 0, "1"));
        System.out.println(jedis.lrange("key10", 0, -1));
        jedis.ltrim("key10", 2, 3);
        System.out.println(jedis.lrange("key10", 0, -1));
        System.out.println(jedis.lset("key10", 1, "10"));
        System.out.println(jedis.lrange("key10", 0, -1));
    }
    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool(Constant.IP);
        try (Jedis jedis = jedisPool.getResource()){
//            test01(jedis);
//            test02(jedis);
//            test03(jedis);
            test04(jedis);
        }
    }
}
